package com.scut.shangxueguan.contrlller.admin;

import com.scut.shangxueguan.pojo.Result;
import com.scut.shangxueguan.service.IAdministratorService;
import jakarta.validation.constraints.Pattern;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RequestMapping("/admin")
@RequiredArgsConstructor
@Validated // 启用@Pattern
public class AdministratorController {

    private final IAdministratorService administratorService;

    @PostMapping("/login")
    public Result<String> login(@Pattern(regexp = "^[a-zA-Z0-9]{6,12}$", message = "用户名必须是6到12位的字母或数字")
                                    String username,
                                @Pattern(regexp ="^\\S{6,20}$", message = "密码必须是6到20位的字符")
                                    String password){
        log.info("用户登录：{}， {}", username, password);
        return administratorService.login(username, password);
    }

    @GetMapping("/logout")
    public Result logout(){
        log.info("用户登出。。。。");
        return administratorService.logout();
    }

    @PutMapping("/updatePassword")
    public Result updatePassword(
            @Pattern(regexp ="^\\S{6,20}$", message = "密码必须是6到20位的字符") String oldPassword,
            @Pattern(regexp ="^\\S{6,20}$", message = "密码必须是6到20位的字符") String newPassword,
            @Pattern(regexp ="^\\S{6,20}$", message = "密码必须是6到20位的字符") String renewPassword
    ){
        log.info("修改密码：{}，{}", oldPassword, newPassword);
        return administratorService.updatePassword(oldPassword, newPassword, renewPassword);
    }
}
